package com.tt.fashionWebSite.controller;

import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.PaymentService;
import com.tt.fashionWebSite.util.ISSConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by isoft-work 5 on 12/21/2014.
 */
@Controller
public class PaymentController {

   @Autowired
   private PaymentService paymentService;

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/insert", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<PaymentDto> emailContact(HttpServletRequest request, @RequestBody PaymentDto payment) {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      // delete session
      PaymentDto paymentDto = paymentService.billPayment(payment, gioHang);
      if(paymentDto != null) {
         s.setAttribute("shopCart", null);
      }
      return new JsonResponseDto<PaymentDto>(paymentDto, JsonResponseCode.SUCCESS);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/payPal/beforeCheckOut", method = RequestMethod.POST)
   public @ResponseBody
   void beforeCheckOut(HttpServletRequest request, HttpServletResponse response, @RequestBody PaymentDto payment) throws ServletException, IOException {
      HttpSession s = request.getSession();
      s.setAttribute("payment", payment);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/payPal/checkOut", method = RequestMethod.POST)
   public @ResponseBody
   void CheckOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      paymentService.payPalCheckOut(request, response, gioHang);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/payPal/success", method = RequestMethod.GET)
   public @ResponseBody
   void paypalSuccess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      PaymentDto payment = (PaymentDto)s.getAttribute("payment");
      paymentService.payPalSuccess(request, response, gioHang, payment);
      // delete session
      s.setAttribute("shopCart", null);
      s.setAttribute("payment", null);
      String url = ISSConstants.HOST + request.getContextPath() + "/jsp/user/user-check-bill.jsp";
      response.sendRedirect(url);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/payPal/cancel", method = RequestMethod.GET)
   public @ResponseBody
   void paypalCancel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      HttpSession s = request.getSession();
      s.setAttribute("payment", null);
      String url = ISSConstants.HOST + request.getContextPath()  + "/jsp/user/user-payment.jsp";
      response.sendRedirect(url);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/baoKim/payment", method = RequestMethod.POST)
   public @ResponseBody
   void baoKimPayment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      paymentService.baoKimPayment(request, response, gioHang);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/baoKim/success", method = RequestMethod.GET)
   public @ResponseBody
   void success(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String idHD = request.getParameter("order_id");
      if(idHD != null && !idHD.equals("")) {
         HttpSession s = request.getSession();
         GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
         PaymentDto payment = (PaymentDto)s.getAttribute("payment");
         paymentService.kimBaoSuccess(idHD, gioHang, payment);
         // delete session
         s.setAttribute("shopCart", null);
         s.setAttribute("payment", null);
         String url = ISSConstants.HOST + request.getContextPath() +"/jsp/user/user-check-bill.jsp";
         response.sendRedirect(url);
      }
      else{
         String url = ISSConstants.HOST + request.getContextPath() +"/jsp/user/user-payment.jsp";
         response.sendRedirect(url);
      }
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/baoKim/cancel", method = RequestMethod.GET)
   public @ResponseBody
   void baoKimCancel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      HttpSession s = request.getSession();
      s.setAttribute("payment", null);
      String url = ISSConstants.HOST + request.getContextPath() +"/jsp/user/user-payment.jsp";
      response.sendRedirect(url);
   }

   @SuppressWarnings("rawtypes")
   @RequestMapping(value = "/payment/nganLuong/checkOut", method = RequestMethod.POST)
   public @ResponseBody
   void nganLuongCheckOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      HttpSession s = request.getSession();
      GioHangDto gioHang = (GioHangDto)s.getAttribute("shopCart");
      paymentService.nganLuongPayment(request, response, gioHang);
   }
}
